package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TMenu;
import hl.pageModel.DataGrid;
import hl.pageModel.Menu;
import hl.service.MenuService;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	private BaseDao<TMenu> menuDao;

	public BaseDao<TMenu> getMenuDao() {
		return menuDao;
	}

	@Autowired
	public void setMenuDao(BaseDao<TMenu> menuDao) {
		this.menuDao = menuDao;
	}

	@Override
	public Object getTreeNode(String pkMenu) {
		List<Menu> nl = new ArrayList<Menu>();
		String hql = null;
		if (pkMenu == null || pkMenu.equals("")) {
			// 查询所有根
			hql = "from TMenu t where t.TMenu is null";
		} else {
			// 异步加载
			hql = "from TMenu t where t.TMenu ='" + pkMenu + "'";
		}
		List<TMenu> l = menuDao.find(hql);
		if (l != null && l.size() > 0) {
			for (TMenu t : l) {
				Menu m = new Menu();
				m.setId(t.getPkMenu());
				m.setText(t.getText());
				m.setIconCls(t.getIcon());
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", t.getUrl());
				m.setAttributes(attributes);
				if (t.getTMenus() != null && !t.getTMenus().isEmpty()) {
					m.setState("closed");
				} else
					m.setState("open");
				nl.add(m);
			}
		}
		return nl;
	}

	@Override
	public DataGrid getDatagrid(Menu menu) {
		DataGrid dg = new DataGrid();
		String hql = "from TMenu t ";

		Map<String, Object> params = new HashMap<String, Object>();
		if (menu.getText() != null && !menu.getText().trim().equals("")) {
			hql += " where t.text like :text ";
			params.put("text", "%" + menu.getText().trim() + "%");
		}
		if (menu.getParent_name() != null && !menu.getParent_name().trim().equals("")) {
			hql += " where t.TMenu.text like :text ";
			params.put("text", "%" + menu.getParent_name().trim() + "%");
		}
		if (menu.getOrder() != null) {
			hql += " order by " + menu.getSort() + " " + menu.getOrder();
		}

		List<TMenu> l = menuDao.find(hql, params, menu.getPage(), menu.getRows());

		int totalHql = menuDao.count(hql, params);

		List<Menu> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TMenu t : l) {
				Menu u = new Menu();
				BeanUtils.copyProperties(t, u);
				if (t.getTMenu() != null) {
					u.setParent_name(t.getTMenu().getText()==null?null:t.getTMenu().getText());
					u.setParent_pk(t.getTMenu().getPkMenu());
				}
				u.setIconCls(t.getIcon());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	@Override
	public void save(Menu menu) {
		TMenu t = new TMenu();
		BeanUtils.copyProperties(menu, t);
		t.setPkMenu(UUID.randomUUID().toString());
		t.setTMenu(menuDao.get(TMenu.class, menu.getParent_pk()));
		t.setIcon(menu.getIconCls());
		menuDao.save(t);
	}

	@Override
	public Object getMenuCombox() {
		String hql = "from TMenu t ";
		List<TMenu> l = menuDao.find(hql);
		List<Menu> nl = new ArrayList<Menu>();
		if (l != null && l.size() > 0) {
			for (TMenu t : l) {
				Menu u = new Menu();
				u.setText(t.getText());
				u.setId(t.getPkMenu());
				nl.add(u);
			}
		}
		return nl;
	}

	@Override
	public void edit(Menu menu) {
		TMenu t = new TMenu();
		BeanUtils.copyProperties(menu, t);
		t.setIcon(menu.getIconCls());
		t.setTMenu(menuDao.get(TMenu.class, menu.getParent_pk()));
		menuDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Menu menu) {
		TMenu t =menuDao.get(TMenu.class, menu.getPkMenu());
		menuDao.delete(t);
	}

}
